package com.dashu.device.service.HF;


import com.dashu.base.bean.device.HF.req.HFReqAddOrUpUser;
import com.dashu.base.bean.device.HF.req.HFReqDelUser;
import com.dashu.base.bean.device.HF.req.HFReqOpenDoor;
import com.dashu.base.bean.device.HF.req.HFReqUserInfo;
import com.dashu.base.enums.device.PicType;
import com.dashu.base.utils.ToEntityAnStr;
import com.dashu.base.utils.encryption.MD5;
import com.dashu.base.vo.*;
import com.dashu.device.api.HF.HFExternalApi;
import com.dashu.device.dto.*;
import com.dashu.device.service.DataService;
import com.dashu.device.util.HFWriteUtil;
import com.dashu.device.vo.RestartReqVO;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import static com.dashu.device.container.CQContainer.CQonlineStr;
import static com.dashu.device.container.HFContainer.HFonlineStr;
import static com.dashu.device.tools.HFMark.*;

@Slf4j
@Service
public class HFInsideExternalService<T> extends DataService implements HFExternalApi<T> {

    /**
     * 创建用户
     */
    @Override
    public DevicePicAddRespDTO<T> addUser(DevicePicAddVO<T> req) {
        log.info("HFAdapter2.addPic:" + req.getDeviceUUID() + "设备");
        ChannelHandlerContext ctx = HFonlineStr.get(req.getDeviceUUID());
        HFReqAddOrUpUser hfReqAddOrUpUser = new HFReqAddOrUpUser();
        hfReqAddOrUpUser.setRequest_type(HF_REQ_ADD);
        hfReqAddOrUpUser.setUser_id(req.getUserId());
        hfReqAddOrUpUser.setImage_type(PicType.URL.name());
        hfReqAddOrUpUser.setImage_content(req.getResp());
        hfReqAddOrUpUser.setPass(MD5.encrypt(req.getPass()));
        hfReqAddOrUpUser.setRequest_id(System.currentTimeMillis());
        HFReqUserInfo hfReqUserInfo = new HFReqUserInfo();
        hfReqUserInfo.setName(req.getUserName());
        hfReqAddOrUpUser.setUser_info(hfReqUserInfo);
        if (ctx != null) {
            HFWriteUtil.send(ctx, ToEntityAnStr.obj2String(hfReqAddOrUpUser));
        }
        DevicePicAddRespDTO<T> addResp = new DevicePicAddRespDTO<>();
        addResp.setMsg("添加成功");
        log.info("HFAdapter2.addPic:");
        return addResp;
    }

    @Override
    public DevicePicDelRespDTO<T> delPic(DevicePicDelReqVO<T> req) {
        log.info("CQAdapter2.delPic:" + req.getDeviceUUID() + "设备");
        ChannelHandlerContext ctx = CQonlineStr.get(req.getDeviceUUID());
        HFReqDelUser hfReqDelUser = new HFReqDelUser();
        hfReqDelUser.setRequest_type(HF_REQ_DEL);
        hfReqDelUser.setUser_id(req.getIden().toString() + "_" + req.getUserId());
        hfReqDelUser.setPass(req.getPass());
        hfReqDelUser.setRequest_id(System.currentTimeMillis());
        HFReqUserInfo hfReqUserInfo = new HFReqUserInfo();
        hfReqUserInfo.setName(req.getUserName());
        if (ctx != null) {
            HFWriteUtil.send(ctx, ToEntityAnStr.obj2String(hfReqDelUser));
        }
        DevicePicDelRespDTO<T> delResp = new DevicePicDelRespDTO<>();
        delResp.setMsg("删除用户");
        log.info("CQAdapte2.delPic:" + delResp.toString());
        return delResp;
    }

    @Override
    public DevicePicUpdRespDTO<T> updPic(DevicePicUpdReqVO<T> req) {
        log.info("HFAdapter2.updPic:" + req.getDeviceUUID() + "设备");
        ChannelHandlerContext ctx = CQonlineStr.get(req.getDeviceUUID());
        HFReqAddOrUpUser hfReqAddOrUpUser = new HFReqAddOrUpUser();
        hfReqAddOrUpUser.setRequest_type(HF_REQ_UP);
        hfReqAddOrUpUser.setUser_id(req.getIden().toString() + "_" + req.getUserId());
        hfReqAddOrUpUser.setImage_type(PicType.URL.name());
        hfReqAddOrUpUser.setImage_content(req.getResp());
        hfReqAddOrUpUser.setPass(req.getPass());
        hfReqAddOrUpUser.setRequest_id(System.currentTimeMillis());
        HFReqUserInfo hfReqUserInfo = new HFReqUserInfo();
        hfReqUserInfo.setName(req.getUserName());
        if (ctx != null) {
            HFWriteUtil.send(ctx, ToEntityAnStr.obj2String(hfReqAddOrUpUser));
        }
        DevicePicUpdRespDTO<T> updResp = new DevicePicUpdRespDTO<>();
        updResp.setMsg("修改用户");
        log.info("HFAdapter2.updPic:" + updResp.toString());
        return updResp;
    }

    @Override
    public DeviceOpenDoorRespDTO<T> openDoor(DeviceOpenDoorReqVO<T> req) {
        log.info("HFAdapter.openDoor:" + req.toString());
        ChannelHandlerContext ctx = CQonlineStr.get(req.getDeviceUUID());
        HFReqOpenDoor hfReqOpenDoor = new HFReqOpenDoor();
        hfReqOpenDoor.setRequest_type(HF_REQ_OPEN_DOOR);
        hfReqOpenDoor.setPass(req.getPass());
        hfReqOpenDoor.setRequest_id(System.currentTimeMillis());
        if (ctx != null) {
            HFWriteUtil.send(ctx, ToEntityAnStr.obj2String(hfReqOpenDoor));
        }
        DeviceOpenDoorRespDTO<T> doorResp = new DeviceOpenDoorRespDTO<>();
        doorResp.setClientUUID(req.getClientUUID());
        doorResp.setMessageId(req.getMessageId());
        doorResp.setT(req.getT());
        doorResp.setModuleType(req.getModuleType());
        log.info("HFAdapter2.openDoor:" + hfReqOpenDoor.toString());
        return doorResp;
    }

    @Override
    public DeviceExistUserRespDTO<T> existUser(DeviceExistUserReqVO<T> req) {

        DeviceExistUserRespDTO<T> delResp = new DeviceExistUserRespDTO<>();
        delResp.setClientUUID(req.getDeviceUUID());
        log.info("CQAdapte2.delPic:" + delResp.toString());
        return delResp;
    }

    @Override
    public boolean restart(RestartReqVO<T> req) {
        ChannelHandlerContext ctx = CQonlineStr.get(req.getDeviceUUID());
        HFReqOpenDoor hfReqOpenDoor = new HFReqOpenDoor();
        hfReqOpenDoor.setRequest_type(HF_REQ_RESTART);
        hfReqOpenDoor.setPass(req.getPass());
        hfReqOpenDoor.setRequest_id(System.currentTimeMillis());
        if (ctx != null) {
            HFWriteUtil.send(ctx, ToEntityAnStr.obj2String(hfReqOpenDoor));
        }
        return true;
    }


}
